package top.maof.book.service.myImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.maof.book.result.PageData;
import top.maof.book.result.PageWrap;
import top.maof.book.utils.Utils;
import top.maof.book.vo.PageVo;
import top.maof.book.dao.CommentMapper;
import top.maof.book.model.Comment;
import top.maof.book.service.CommentService;
import top.maof.book.vo.CommentVo;

import java.util.List;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    @Autowired
    CommentMapper commentMapper;

    @Override
    public PageData<Comment> findPage(PageWrap<Comment> pageWrap) {
        IPage<Comment> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());
        if (pageWrap.getModel().getId() != null) {
            queryWrapper.lambda().eq(Comment::getId, pageWrap.getModel().getId());
        }
        if (pageWrap.getModel().getContent() != null) {
            queryWrapper.lambda().eq(Comment::getContent, pageWrap.getModel().getContent());
        }
        if (pageWrap.getModel().getUserId() != null) {
            queryWrapper.lambda().eq(Comment::getUserId, pageWrap.getModel().getUserId());
        }
        if (pageWrap.getModel().getBookId() != null) {
            queryWrapper.lambda().eq(Comment::getBookId, pageWrap.getModel().getBookId());
        }
        if (pageWrap.getModel().getReply() != null) {
            queryWrapper.lambda().eq(Comment::getReply, pageWrap.getModel().getReply());
        }
        if (pageWrap.getModel().getCommunication() != null) {
            queryWrapper.lambda().eq(Comment::getCommunication, pageWrap.getModel().getCommunication());
        }
        if (pageWrap.getModel().getTime() != null) {
            queryWrapper.lambda().eq(Comment::getTime, pageWrap.getModel().getTime());
        }
        for(PageWrap.SortData sortData: pageWrap.getSorts()) {
            if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) {
                queryWrapper.orderByDesc(sortData.getProperty());
            } else {
                queryWrapper.orderByAsc(sortData.getProperty());
            }
        }
        return PageData.from(commentMapper.selectPage(page, queryWrapper));
    }


    @Override
    public Comment find(Integer id) {
        return commentMapper.find(id);
    }

    @Override
    public int insert(Comment c) {
        return commentMapper.insert(c);
    }

    @Override
    public PageVo list(Integer bookId, Integer page, Integer pageSize) {
        PageVo pageBo = new PageVo();
        int i = commentMapper.listCount(bookId);
        List<CommentVo> list = commentMapper.list(bookId, (page - 1) * pageSize, pageSize);
        pageBo.setCount(i);
        pageBo.setList(list);
        return pageBo;
    }

    @Override
    public CommentVo get(Integer id) {
        return commentMapper.get(id);
    }
}
